def cocktailSort(arr):
    n = len(arr)
    start = 0
    end = n - 1
    count = 0
    while start < end:
        swapped = False
        # 从左到右冒泡
        for i in range(start, end):
            if arr[i] > arr[i + 1]:
                arr[i], arr[i + 1] = arr[i + 1], arr[i]
                swapped = True
        if not swapped:
            break
        end -= 1
        swapped = False
        # 从右到左冒泡
        for i in range(end, start, -1):
            if arr[i] < arr[i - 1]:
                arr[i], arr[i - 1] = arr[i - 1], arr[i]
                swapped = True
        if not swapped:
            break
        count += 1
        start += 1
    print(f"运行次数: {count}")
    return arr

arr = [5, 4, 3, 2, 1]
sorted_arr = cocktailSort(arr)
print(sorted_arr)